home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15151 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: solon.com!not-for-mail
  2. From: The Amorphous Mass <robinson@blue.weeg.uiowa.edu>
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: Re: fflush(stdin) - not guaranteed to work?
  5. Date: 17 Apr 1996 10:44:10 -0500
  6. Organization: University of Iowa, Iowa City, IA, USA
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4l33ka$7n0@solutions.solon.com>
  10. References: <4ksjpn$rjt@solutions.solon.com> <4ku3id$53h@solutions.solon.com> <4l1idn$pvs@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Sender: robinson@green.weeg.uiowa.edu
  13.  
  14. On 16 Apr 1996, News User Id wrote:
  15. > To the implicit followon question, "If
  16. > fflush(stdin) won't work, how *can* I flush the user's typeahead
  17. > before issuing a prompt confirming whether I should reformat the
  18. > hard drive?", the FAQ list suggests no answer, because I don't
  19. > know a good one (other than, your program probably shouldn't be
  20. > offering to do that at all).
  21.  
  22.   Since you offer answers to questions that might work on most 
  23. implementations (eg the definition of the offsetof() macro) you might 
  24. suggest a variation on
  25.  
  26. void Flush()
  27. {
  28.     while(getchar() != '\n')
  29.         ;
  30. }
  31.  
  32.   which is only called if the last character read was not a newline.  
  33. This has yet to fail on the systems I've tested it on, but that obviously 
  34. doesn't mean that it always works.  Can anyone get it to fail to 
  35. transparently clear stdin (by, say, requuiring that the user hit <CR>)?  
  36. Remember to only call it if the last character read was not a newline. :-)
  37.  
  38. --
  39. James Robinson            "Because we are returning a copy for postfix ++ 
  40. james-robinson@uiowa.edu  expressions, statements such as (c++)++; won't
  41. robinson@cs.uiowa.edu     work as expected."            -- Weiskamp & Flamig,
  42.                                            _The_Complete_C++_Primer_, 2nd ed.
  43.